use crate::solution::Solution;

impl Solution {
    // 贪心 O(m+n)
    pub fn restore_matrix(mut row_sum: Vec<i32>, mut col_sum: Vec<i32>) -> Vec<Vec<i32>> {
        // warning: value assigned to `minimum` is never read
        // warning: `rust-algorithms` (bin "rust-algorithms" test) generated 1 warning
        // warning: value assigned to `minimum` is never read
        // let (mut i, mut j, m, n, mut minimum) = (0, 0, row_sum.len(), col_sum.len(), 0);
        let (mut i, mut j, m, n) = (0, 0, row_sum.len(), col_sum.len());
        let mut grid = vec![vec![0; n]; m];
        while i < m && j < n {
            let minimum = row_sum[i].min(col_sum[j]);
            grid[i][j] = minimum;
            row_sum[i] -= minimum;
            col_sum[j] -= minimum;
            if row_sum[i] == 0 { i += 1; }
            if col_sum[j] == 0 { j += 1; }
        }
        grid
    }

    // 贪心 O(m∗n)
    // pub fn restore_matrix_2(mut row_sum: Vec<i32>, mut col_sum: Vec<i32>) -> Vec<Vec<i32>> {
    //     let (m, n, mut minimum) = (row_sum.len(), col_sum.len(), 0);
    //     let mut grid = vec![vec![0; n]; m];
    //     for i in 0..m {
    //         for j in 0..n {
    //             minimum = row_sum[i].min(col_sum[j]);
    //             grid[i][j] = minimum;
    //             row_sum[i] -= minimum;
    //             col_sum[j] -= minimum;
    //         }
    //     }
    //     grid
    // }

}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_1() {
        let row_sum = [3,8].to_vec();
        let col_sum = [4,7].to_vec();
        let output = [[3,0].to_vec(),[1,7].to_vec()].to_vec();

        let ans = Solution::restore_matrix(row_sum, col_sum);
        println!("{:?}", ans);
        assert_eq!(ans, output);
    }
}
